home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_usrdoc / SVGALIB-.{2E / SVPMI / PARSE.C < prev    next >
C/C++ Source or Header  |  1999-09-17  |  5KB  |  254 lines

  1.  
  2. /*
  3.  * This is a quickly hacked program to convert an SVPMI (Super VGA Protected
  4.  * Mode Interface) data file to an svgalib driver. Feedback is
  5.  * very welcome.
  6.  *
  7.  * Initial version (Mar 94 HH). Doesn't do much yet.
  8.  * Assumes textmode is last defined mode.
  9.  * End with ctrl-c. Correct resulting files by hand.
  10.  * (add "}" to modetable.svpmi)
  11.  */
  12.  
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18.  
  19. void parse ();
  20.  
  21.  
  22. void
  23. main ()
  24. {
  25.   parse ();
  26. }
  27.  
  28.  
  29. /* Return pointer to line red from stdin, end marked by CR or CR/LF.
  30.  * Initial spaces are removed. */
  31.  
  32. char *
  33. getline ()
  34. {
  35.   static char linebuf[128];
  36.   int i, length, spaces;
  37.   i = 0;
  38.   for (;;)
  39.     {
  40.       int c;
  41.       c = fgetc (stdin);
  42.       if (feof (stdin))
  43.     return NULL;
  44.       if (c == 13)
  45.     continue;        /* Skip. */
  46.       if (c == 10)
  47.     break;
  48.       linebuf[i] = c;
  49.       i++;
  50.     }
  51.   length = i;
  52.   linebuf[i] = 0;
  53.   /* Remove initial spaces. */
  54.   spaces = 0;
  55.   i = 0;
  56.   while (i < length)
  57.     {
  58.       if (linebuf[i] != ' ')
  59.     break;
  60.       i++;
  61.       spaces++;
  62.     }
  63.   return linebuf + spaces;
  64. }
  65.  
  66.  
  67. /* Skip lines until left side of line matches string s. */
  68.  
  69. char *
  70. getthisline (char *s)
  71. {
  72.   char buf[128];
  73.   int n;
  74.   n = strlen (s);
  75.   for (;;)
  76.     {
  77.       char *line;
  78.       line = getline ();
  79.       if (strncmp (line, s, n) == 0)
  80.     return line;
  81.     }
  82. }
  83.  
  84.  
  85. /* Get the (n + 1)th word delimited by ' ' and ';' in string s. */
  86.  
  87. char *
  88. getword (char *s, int n)
  89. {
  90.   int i;
  91.   char *word;
  92.   int mode, wcount;
  93.   word = s;
  94.   mode = 0;            /* Whitespace. */
  95.   wcount = 0;
  96.   i = 0;
  97.   for (i = 0; s[i] != 0; i++)
  98.     {
  99.       if (s[i] == ' ' || s[i] == ';')
  100.     if (mode == 0)
  101.       continue;
  102.     else
  103.       {
  104.         s[i] = 0;
  105.         if (wcount == n)
  106.           return word;
  107.         wcount++;
  108.         mode = 0;
  109.       }
  110.       else if (mode == 1)
  111.     continue;
  112.       else
  113.     {
  114.       word = &s[i];
  115.       mode = 1;
  116.     }
  117.     }
  118.   return NULL;
  119. }
  120.  
  121.  
  122. /* Write lines to file until left part matches string s. */
  123.  
  124. void
  125. writetofileuntilthisline (char *s, FILE * f)
  126. {
  127.   int n;
  128.   n = strlen (s);
  129.   for (;;)
  130.     {
  131.       char *line;
  132.       line = getline ();
  133.       if (strncmp (line, s, n) == 0)
  134.     break;
  135.       fprintf (f, "%s\n", line);
  136.     }
  137. }
  138.  
  139.  
  140. void
  141. writetofileuntilend (FILE * f)
  142. {
  143.   for (;;)
  144.     {
  145.       char *line;
  146.       line = getline ();
  147.       if (line == NULL)
  148.     return;
  149.       fprintf (f, "%s\n", line);
  150.     }
  151. }
  152.  
  153.  
  154. void
  155. parse ()
  156. {
  157.   char *line;
  158.   char s[128];
  159.   char modename[40];
  160.   int modenumber;
  161.   FILE *f, *g;
  162.   printf ("SVPMI to svgalib driver converter.\n\n");
  163.  
  164.   /* Read header. */
  165.   getthisline ("[ADAPTER]");
  166.   line = getline ();
  167.   printf ("Graphics Adapter string: %s\n", line);
  168.  
  169.   /* Read modes. */
  170.   modenumber = 0;
  171.   g = fopen ("modetable.svpmi", "wb");
  172.   f = fopen ("modes.svpmi", "wb");
  173.   fprintf (g, "/* svgalib SVPMI mode table. */\n\n");
  174.   fprintf (g, "static svpmi_modeentry svpmi_modes[] = {\n");
  175.  
  176.   for (;;)
  177.     {
  178.       int XResolution;        /* SVPMI modeinfo fields. */
  179.       int YResolution;
  180.       int BitsPerPixel;
  181.       int BytesPerScanline;
  182.       int WinAGranularity;
  183.       int WinASize;
  184.       int WinABase;
  185.       int ModeAttributes;
  186.       getthisline ("[MODEINFO]");
  187.       line = getthisline ("ModeAttributes");
  188.       ModeAttributes = atoi (getword (line, 2));
  189.       line = getthisline ("WinAGranularity");
  190.       WinAGranularity = atoi (getword (line, 2));
  191.       line = getthisline ("WinASize");
  192.       WinASize = atoi (getword (line, 2));
  193.       line = getthisline ("WinABase");
  194.       WinABase = atoi (getword (line, 2));
  195. #if 0
  196.       if (WinABase != 0xa0000)
  197.     {
  198.       printf ("Window is not at 0xa0000.\n");
  199.       exit (-1);
  200.     }
  201. #endif
  202.       line = getthisline ("BytesPerScanline");
  203.       BytesPerScanline = atoi (getword (line, 2));
  204.       line = getthisline ("XResolution");
  205.       XResolution = atoi (getword (line, 2));
  206.       line = getthisline ("YResolution");
  207.       YResolution = atoi (getword (line, 2));
  208.       line = getthisline ("BitsPerPixel");
  209.       BitsPerPixel = atoi (getword (line, 2));
  210.  
  211.       if (ModeAttributes == 0x07)
  212.     {
  213.       /* Text mode. */
  214.       printf ("Textmode found.\n");
  215.       getthisline ("[SETMODE]");
  216.       fprintf (f, "static void svpmi_setmode_text() {\n");
  217.       writetofileuntilend (f);
  218.       fprintf (f, "}\n\n\n");
  219.       fprintf (g, "}\n");
  220.       fclose (f);
  221.       fclose (g);
  222.       exit (0);
  223.     }
  224.  
  225.       printf ("Mode found: %d x %d, %dbpp  %d bpl  gran A %d\n",
  226.           XResolution, YResolution, BitsPerPixel,
  227.           BytesPerScanline, WinAGranularity);
  228.  
  229.       sprintf (modename, "%dx%dx%d", XResolution, YResolution,
  230.            1 << BitsPerPixel);
  231.  
  232.       getthisline ("[SETMODE]");
  233.       fprintf (f, "static void svpmi_setmode_%s() {\n", modename);
  234.       writetofileuntilthisline ("[SETCOLOR]", f);
  235.       fprintf (f, "}\n\n\n");
  236.       getthisline ("[SETWINDOW]");
  237.       fprintf (f, "static void svpmi_setwindow_%s( int r0 ) {\n", modename);
  238.       writetofileuntilthisline ("[MODE]", f);
  239.       fprintf (f, "}\n\n\n");
  240.  
  241.       fprintf (g, "{ %d, %d, %d, %d, %d, svpmi_setmode_%s, svpmi_setwindow_%s },\n",
  242.            XResolution, YResolution, BitsPerPixel,
  243.            BytesPerScanline, WinAGranularity, modename, modename);
  244.  
  245.       fflush (f);
  246.       fflush (g);
  247.  
  248.       modenumber++;
  249.     }
  250.   fprintf (g, "}\n");
  251.   fclose (f);
  252.   fclose (g);
  253. }
  254.